/*
 * enhancement.cpp
 *
 *  Created on: 2011-3-1
 *      Author: tom
 */

#include "enhancement.h"

Enhancement::Enhancement(const IplImage* const image){
	this->image=cvCreateImage(cvGetSize(image),image->depth,image->nChannels);
	cvCopy(image,this->image);
}

Enhancement::~Enhancement(){
	cvReleaseImage(&image);
}

void Enhancement::hEqualization(){
	IplImage* dst=cvCreateImage(cvGetSize(image),IPL_DEPTH_8U,3);
	cvSetZero(dst);

	IplImage* red=cvCreateImage(cvGetSize(image),IPL_DEPTH_8U,1);
	IplImage* green=cvCreateImage(cvGetSize(image),IPL_DEPTH_8U,1);
	IplImage* blue=cvCreateImage(cvGetSize(image),IPL_DEPTH_8U,1);
	cvSplit(image,red,green,blue,0);

	IplImage* rDst=cvCreateImage(cvGetSize(red),red->depth,red->nChannels);
	IplImage* gDst=cvCreateImage(cvGetSize(green),green->depth,green->nChannels);
	IplImage* bDst=cvCreateImage(cvGetSize(blue),blue->depth,blue->nChannels);

	cvEqualizeHist(red,rDst);
	cvEqualizeHist(green,gDst);
	cvEqualizeHist(blue,bDst);

	for(int row=0;row<dst->height;row++){
		char* ptr=dst->imageData+row*dst->widthStep;
		char* rPtr=rDst->imageData+row*rDst->widthStep;
		char* gPtr=gDst->imageData+row*gDst->widthStep;
		char* bPtr=bDst->imageData+row*bDst->widthStep;

		for(int col=0;col<dst->width;col++){
			ptr[3*col]=rPtr[col];
			ptr[3*col+1]=gPtr[col];
			ptr[3*col+2]=bPtr[col];
		}
	}

	cvNamedWindow("Histogram Equalization");
	cvShowImage("Histogram Equalization",dst);
	cvWaitKey(0);
	cvReleaseImage(&dst);
	cvReleaseImage(&red);
	cvReleaseImage(&green);
	cvReleaseImage(&blue);
	cvReleaseImage(&rDst);
	cvReleaseImage(&gDst);
	cvReleaseImage(&bDst);
	cvDestroyWindow("Histogram Equalization");
}

void Enhancement::lSharpen(int size){
	IplImage* dst=cvCreateImage(cvGetSize(image),IPL_DEPTH_16S,3);
	cvLaplace(image,dst,size);
	cvNamedWindow("Laplace Sharpen");
	cvShowImage("Laplace Sharpen",dst);
	cvWaitKey(0);
	cvReleaseImage(&dst);
	cvDestroyWindow("Laplace Sharpen");
}

void Enhancement::gFilter(int row,int col){
	IplImage* dst=cvCreateImage(cvGetSize(image),image->depth,image->nChannels);
	cvSmooth(image,dst,CV_GAUSSIAN,row,col);
	cvNamedWindow("Gaussian");
	cvShowImage("Gaussian",dst);
	cvWaitKey(0);
	cvReleaseImage(&dst);
	cvDestroyWindow("Gaussian");
};
